<?php
define ('TEST_USER_ID', 2);
require_once '../common.php';

if ($_POST['item_image'])
{
	$user_id = user::getId();
	if (empty($user_id))
	{
		echo json_encode(array("result"=>false, "message"=>"请先登录。"));
		exit;
	}

	$post_info = $_POST;

	$item_image_name = $post_info['item_image'];
	$item_image_original = ITEM_IMAGE_UPLOAD_DIR.'/'.$item_image_name;
	if (!file_exists($item_image_original))
	{
		echo json_encode(array("result"=>false, "message"=>"上传文件失败。"));
		exit;
	}

	$item_id = item::add($post_info);
	if (!$item_id)
	{
		echo json_encode(array("result"=>false, "message"=>"写入数据库失败。".$item_id));
		exit;
	}
	
	if (!itemimage::save($item_id, $item_image_original))
	{
		echo json_encode(array("result"=>false, "message"=>"保存图片文件失败。"));exit;
	}
	
	imgcache::deleteMapBrick(floor($post_info['item_x']/CELL_COUNT_OF_BRICK_SIDE),
		floor($post_info['item_y']/CELL_COUNT_OF_BRICK_SIDE));

	echo json_encode(array("result"=>true, "item_id"=>$item_id));
}
else
{
	echo json_encode(array("result"=>false, "message"=>"信息不完整。"));
	exit;
}
/*
CREATE TABLE `test_items` (
  `item_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `item_image` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `item_x` int(11) unsigned,
  `item_y` int(11) unsigned,
  PRIMARY KEY (`item_id`),
  KEY (`item_x`),
  KEY (`item_y`),
  UNIQUE KEY item_x_y (`item_x`, `item_y`)
) ENGINE=MyISAM AUTO_INCREMENT=10000 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

*/